package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.ResponseConstant;
import com.itany.exception.ManagerUserExistsException;
import com.itany.exception.RequestParamsException;
import com.itany.exception.WrongUsernameOrPasswordException;
import com.itany.pojo.ManagerUser;
import com.itany.service.ManagerUserService;
import com.itany.vo.ActionResult;
import com.itany.vo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager")
public class ManagerUserController {
    @Autowired
    ManagerUserService managerUserService;


    /**
     * 添加管理员
     *
     * @param username 管理员名称
     * @param password 管理员密码
     * @param roles    管理员角色
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ActionResult addManagerUser(String username, String password, String supplierId,
                                       @RequestParam(value = "roles[]", required = false) Integer[] roles) {
        ActionResult result = new ActionResult();
        try {
            managerUserService.addManagerUser(username, password, supplierId, roles);
            result.setMsg("添加成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (ManagerUserExistsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }


    /**
     * 修改管理员信息
     *
     * @param id       管理员id
     * @param username 管理员名称
     * @param password 管理员密码
     * @param roles    管理员角色
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public ActionResult modifyManagerUser(String id, String username, String password, String supplierId,
                                          @RequestParam(value = "roles[]", required = false) Integer[] roles) {
        ActionResult result = new ActionResult();
        try {
            managerUserService.modifyManagerUser(id, username, password, supplierId, roles);
            result.setMsg("修改成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (ManagerUserExistsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }

    /**
     * 查找所有管理员
     *
     * @param page 页码数
     * @param rows 每页数据条数
     * @return 分页管理员信息
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows,
                                       String managernameKey) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<ManagerUser> info = null;
        try {
            info = managerUserService.findAll(page, rows, managernameKey);
        } catch (RequestParamsException e) {
            e.printStackTrace();
        }
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    /**
     * 后台登录
     *
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping(value = "/login", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView login(String username, String password, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        try {
            ManagerUser login = managerUserService.login(username, password);
            session.setAttribute("login", login);
            mav.setViewName("redirect:/manager/showindex");
        } catch (RequestParamsException | WrongUsernameOrPasswordException e) {
            mav.addObject("loginMsg", e.getMessage());
            mav.setViewName("login");
        } catch (Exception e) {
            mav.addObject("loginMsg", "服务器内部异常");
            mav.setViewName("login");
        }
        return mav;
    }


    @RequestMapping( "/logout")
    public String logout(HttpSession session){
            session.removeAttribute("login");
            return "redirect:/manager/showlogin";
    }


    /**
     * 动态生成导航菜单
     */
    @RequestMapping("/showMenus")
    @ResponseBody
    public ActionResult showMenus(String managerUserId,HttpSession session){
        ActionResult result = new ActionResult();
        try {
            List<Menu> menus = managerUserService.generateMenus(managerUserId);
            result.setData(menus);
            session.setAttribute("menus",menus);
            result.setMsg("生成菜单成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }

    /**
     * 修改密码
     */
    @RequestMapping("/modifyPassword")
    @ResponseBody
    public ActionResult modifyPassword(String newpass,String managerUserId){
        ActionResult result = new ActionResult();
        try {
            managerUserService.modifyPassword(newpass,managerUserId);
        } catch (RequestParamsException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }



}
